<?php

function list_potongan($kspes,$knik,$kbulan,$ktahun,$kstatus){
	my_set_code_js('
		function confirmDelete(id){
			var t = confirm(\'Yakin akan menghapus data ?\');
			if(t){
				location.href=\'index.php?com='.$_GET['com'].'&task=delete&id=\'+id;
			}
			return false;
		}
	');

	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:2%'),  
		'Spesifikasi'=>array('style'=>'border-bottom:2px solid;width:24%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:9%'),  
		'Karyawan'=>array('style'=>'border-bottom:2px solid;width:19%'),  
		'Mulai'=>array('style'=>'border-bottom:2px solid;width:7%'),  
		'akhir'=>array('style'=>'border-bottom:2px solid;width:7%'),  
		'Setor '=>array('style'=>'border-bottom:2px solid;width:6%'),  
		'Total'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),  
		'Status'=>array('style'=>'text-align:right;border-bottom:2px solid;width:9%'),  
		' '=>array('style'=>'border-bottom:2px solid;width:8%'), 
	);
	$query = "SELECT * ,a.tanggal_selesai AS tgl_end FROM pr_potongan_limit a
			LEFT JOIN pr_komponen_gaji b ON a.komponen_gaji_id=b.komponen_gaji_id 
			INNER JOIN   karyawan c  ON a.karyawan_id = c.karyawan_id
			INNER JOIN biodata d ON d.biodata_id = c.biodata_id
			WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
			AND b.model_entry = 6";		
	if($kspes<>0){
		$query .= " AND a.komponen_gaji_id={$kspes}";
	}
	if($knik<>0){
		$query .= " AND c.nik={$knik}";
	}
	if($kbulan<>0 AND $ktahun<>''){
		$query .= " AND ('{$ktahun}-{$kbulan}-01' BETWEEN a.tanggal_mulai AND a.tanggal_selesai)";
	}
	$periode_gaji_id =get_periode_gaji();
	$periodes = my_get_data_by_id('pr_periode','periode_id',$periode_gaji_id);
	if($kstatus==1){
		$query .= " AND a.tanggal_selesai<'{$periodes['tanggal']}'";
	}
	if($kstatus==2){
		$query .= " AND a.tanggal_selesai>='{$periodes['tanggal']}' AND a.aktif='Y'";
	}	
	if($kstatus==3){
		$query .= " AND a.tanggal_selesai>='{$periodes['tanggal']}' AND a.aktif<>'Y'";
	}	
	$query .= " ORDER BY c.nik ASC ";
	//var_dump($query); exit;
	$result = my_query($query );
	$total_records = my_num_rows($result );
	$scroll_page =5;  
	$per_page =25;  
	$current_page = isset($_GET['page']) ? (int) $_GET['page'] : 1 ; 
	if($current_page < 1){
		$current_page = 1;
	}		 
	$pager_url  ="index.php?com={$_GET['com']}&kspes={$kspes}&knik={$knik}&kbulan={$kbulan}&ktahun={$ktahun}&kstatus={$kstatus}&page=" ;	 
	$inactive_page_tag = 'style="padding:4px;background-color:#BBBBBB"';  
	$previous_page_text = ' Mundur '; 
	$next_page_text = ' Maju ';  
	$first_page_text = ' Awal '; 
	$last_page_text = ' Akhir ';
	$pager_url_last = isset($pager_url_last) ? $pager_url_last : NULL;
	$kgPagerOBJ = new kgPager();
	$kgPagerOBJ->pager_set(
		$pager_url, 
		$total_records, 
		$scroll_page, 
		$per_page, 
		$current_page, 
		$inactive_page_tag, 
		$previous_page_text, 
		$next_page_text, 
		$first_page_text, 
		$last_page_text ,$pager_url_last); 
	 		
	$result = my_query($query ." LIMIT ".$kgPagerOBJ->start.", ".$kgPagerOBJ->per_page); 	
	$row = array(); 
	$i = ($current_page  - 1 ) * 25; 
	while($ey = my_fetch_array($result)){
		$karyawan=loaddata_karyawan( $ey['karyawan_id']);
		$i++;
		
		$detailproperty = array(
			'href'=>'index.php?com='.$_GET['com'].'&task=pr_potongan_limit&task=detail&id='.$ey['potongan_limit_id'].'&kspes='.$kspes.'&knik='.$knik.'&kbulan='.$kbulan.'&ktahun='.$ktahun.'&kstatus='.$kstatus,
			'title'=>'Detail',
		);
		$detail_button = button_icon( 'b_props.png' , $detailproperty  );
		
		$editproperty = array(
				'href'=>'index.php?com=pr_potongan_limit&task=edit&id='.$ey['potongan_limit_id'],
				'rel'=>'facebox',
				'title'=>'Edit'
		);
		$edit_button = button_icon( 'b_edit.png' , $editproperty  );
		$deleteproperty = array(
				'href'=>'javascript:; ',
				'onclick'=>'javascript:confirmDelete('.$ey['potongan_limit_id'].');',
				'title'=>'Delete'
		);
		$delete_button = button_icon( 'b_drop.png' , $deleteproperty  );
		
		if( $ey['tgl_end'] < $periodes['tanggal']){
				$status="Lunas" ;
		}else{
			if($ey['aktif']=="Y"){
				$status="Belum Lunas" ;
			}else{
				$status="Percepatan" ;			
			}
		}
		$perbulan = ceil( $ey['total'] / $ey['waktu'] );
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'spec'=>  $ey['nama_komponen_gaji'] ,   
			'nik'=>  $karyawan['nik'] ,   
			'nama'=>  $karyawan['nama_gelar'] ,   
			'mulai'=>  position_text_align ( date("m-Y", strtotime($ey['tanggal_mulai'] ." 00:00:00" ) ), 'left'), 
			'selesai'=>  position_text_align ( date("m-Y", strtotime($ey['tgl_end'] ." 00:00:00" ) ), 'left'), 
			'perbulan'=>  position_text_align ( rp_format($perbulan),'right') , 
			'total'=>  position_text_align( rp_format($ey['total']),'right') ,
			'aktif'=> position_text_align( $status,'center') ,
			'operasi'=> position_text_align(  $edit_button.' '.$detail_button.' '.$delete_button , 'right'),  
		);
		 
	}
	
	$datas = table_rows($row); 
	$menuontop = array(  
		'Cetak' => array('onclick'=>'javascript:window.open(\'index.php?com='.$_GET['com'].'&task=plaincetak&kspes='.$kspes.'&knik='.$knik.'&kbulan='.$kbulan.'&ktahun='.$ktahun.'&kstatus='.$kstatus.'\',\'mywin\',\'left=20,top=20,width=800,height=600,toolbar=0,scrollbars,resizable=0\');'),
		'Excel'=>array('onclick'=>'javascript:location.href=\'index.php?com='.$_GET['com'].'&task=excel&kspes='.$kspes.'&knik='.$knik.'&kbulan='.$kbulan.'&ktahun='.$ktahun.'&kstatus='.$kstatus.'\';'),
		'Tambah data'=>array('onclick'=>'javascript: jQuery.facebox({ ajax: \'index.php?com='.$_GET['com'].'&task=edit&id=0\' });')
	);

	$gols = array();
	$query_golongan = "SELECT * FROM pr_komponen_gaji WHERE model_entry = 6";
	$re = my_query($query_golongan);
	while($row = my_fetch_array($re) ){
		$gols[$row['komponen_gaji_id']] =$row['nama_komponen_gaji'];
	 }
	$gol = array(
		'name'=>'kspes',
		'id'=>'kspes',
		'value'=>isset($_GET['kspes']) ? $_GET['kspes'] : 0 
	);
	$bulans  = array(
		'01'=>'01', 
		'02'=>'02', 
		'03'=>'03', 
		'04'=>'04', 
		'05'=>'05', 
		'06'=>'06', 
		'07'=>'07', 
		'08'=>'08', 
		'09'=>'09', 
		'10'=>'10', 
		'11'=>'11', 
		'12'=>'12', 
	);
	$bulan = array(
		'name'=>'kbulan',
		'value'=>isset($_GET['kbulan']) ? $_GET['kbulan']: ''  
	);
	$bulan_form = form_dropdown( $bulan,$bulans);
	
	$tahun = array(
		'name'=>'ktahun','size'=>5,
		'value'=>isset($_GET['ktahun']) ? $_GET['ktahun']: ''
	); 
	$tahun_form = form_dynamic($tahun );
	$temp=array();
	$temp[1]="Lunas" ;
	$temp[2]="Belum Lunas" ;
	$temp[3]="Percepatan" ;
	$form_status = array(
		'name'=>'kstatus',
		'id'=>'kstatus',
		'value'=>isset($_GET['kstatus']) ? $_GET['kstatus'] : 0 
	);	
	$search = '<form method="GET"><input type="hidden" name="com" value="'.$_GET['com'].'" />
	<span style="font:10px verdana"><b>Spesifikasi</b></span>'.form_dropdown($gol , $gols).'
	<span style="font:10px verdana"><b>NIK</b></span>
	<input type="text"  style="width:70px;" name="knik" value="'.(isset($_GET['knik']) ? $_GET['knik']: "").'" />
	<span style="font:10px verdana"><b>Periode</b></span>'.$bulan_form.$tahun_form.'
	<span style="font:10px verdana"><b>Status</b></span>'.form_dropdown($form_status , $temp).'
	<input class="button" type="submit" value=" Cari " /> </form>';

	$printmenu = additional_menu_on_list( $menuontop , array()  );
	$paging = $kgPagerOBJ ->showPaging();
	return $search.$printmenu .table_builder($header , $datas ,  5 , false,$paging );
} 

function form_potongan_limit( $id ){
	my_set_file_js(
		array(
			'components/system/jquery/combomulti/jquery.chainedSelects.js',
			'components/system/js/calendar/calendarDateInput.js' 
		)
	);

	my_set_file_js(
		array(
			'components/system/jquery/autocomplete/jquery.autocomplete.js'  
		)
	);
	my_set_file_css(
		array(
				'components/system/jquery/autocomplete/jquery.autocomplete.css' 
			)
	);

	my_set_code_js('  
		function findValue(li) {
			if( li == null ) return alert("No match!"); 
			if( !!li.extra ) var sValue = li.extra[0]; 
			else var sValue = li.selectValue;
		}

		function selectItem(li) {
			findValue(li);
		}

		function formatItem(row) {
			return   row[0] ;
		}

		function lookupAjax(){
			var oSuggest = $("#nama_karyawan")[0].autocompleter;
			oSuggest.findValue(); 
			return false;
		}
		$(document).ready(function() {
			$("#nama_karyawan").autocomplete(
				"autocomplete_daftar_karyawan.php",
				{
					delay:10,
					minChars:2,
					matchSubset:1,
					matchContains:1,
					cacheLength:5,
					onItemSelect:selectItem,
					onFindValue:findValue,
					formatItem:formatItem,
					autoFill:true
				}
			);
			 
		});

		function checkName(name){
			$.get("check_karyawan_names.php", { nama: name  },
			   function(data){
				 if(data ==  \'0\' ){
					alert(\'Nama karyawan \'+ name+\' tidak ditemukan\nHarap diperiksa kembali\');
					$(\'#nama_karyawan\').val(\'\');
					return false;
				 } 
			   });
		}
		
		function checkValue(){
			var bulan = $(\'#bulan\').val();
			var xtahun = $(\'#tahun\').val();
			var bulan_end = $(\'#bulan_s\').val();
			var tahun_end = $(\'#tahun_s\').val(); 
			var total = $(\'#total\').val(); 
			var bulanan = $(\'#bulanan\').val(); 
			var frekuensi = $(\'#frekuensi\').val(); 
			 
			$.get("get_kalkulasi_potongan.php", { 
				bulan: bulan , 	tahun: xtahun , 	
				bulan_end: bulan_end , 	tahun_end: tahun_end , 	
				bulanan: bulanan , total: total,
				frekuensi: frekuensi
			},
			   function(data){
				    var t = data.split("|");
					//KOMPOSISI BSTART|YSTART|BULAN_END|TAHUN_END|FREKUENSI|TOTAL|NILAI_STOR_BULANAN
					$(\'#total\').val(t[5]); 
					$(\'#bulanan\').val(t[6]); 
					$(\'#frekuensi\').val(t[4]); 
					$(\'#bulan_s\').val(t[2]);
					$(\'#tahun_s\').val(t[3]); 
					return false;
				  
			   });
		}
	');	
	
	$view = form_header( "seleski" , "seleski"  );
	if($id>0){
		$fields = my_get_data_by_id('pr_potongan_limit','potongan_limit_id',$id);
		$month = date('m',strtotime($fields['tanggal_mulai']));
		$year = date('Y',strtotime($fields['tanggal_mulai']));	
		$month_s = date('m',strtotime($fields['tanggal_selesai']));
		$year_s = date('Y',strtotime($fields['tanggal_selesai']));	
		$pot_aktif = $fields['aktif'] ;
	}else{
		$month = date('m');
		$year = date('Y');
		$month_s = date('m');
		$year_s = date('Y');
		$pot_aktif = 'Y' ;
	} 

	$gols = array();
	$query_golongan = "SELECT * FROM pr_komponen_gaji WHERE model_entry = 6";
	$re = my_query($query_golongan);
	 while($row = my_fetch_array($re) ){
		$gols[$row['komponen_gaji_id']] =$row['nama_komponen_gaji'];
	 }
	 $gol = array(
		'name'=>'komponen_gaji_id',
		'id'=>'komponen_gaji_id',
		'value'=>isset($_POST['komponen_gaji_id']) ? $_POST['komponen_gaji_id'] : $fields['komponen_gaji_id'] 
		);
	$form_field_gol = form_dropdown($gol , $gols);
	$view .= form_field_display( $form_field_gol   , "Spesifikasi");

	$karyawan_data = loaddata_karyawan(( $fields ? $fields['karyawan_id'] : 0));
	$karyawan = array(
		'name'=>'nama_karyawan',
		'value'=>(isset($_POST['nama_karyawan'])? $_POST['nama_karyawan'] : ( $fields ? $karyawan_data['nik'].'/'.$karyawan_data['nama'] :'') ),
		'id'=>'nama_karyawan',
		'type'=>'textfield',
		'size'=>'45'
	);
	$form_karyawan = form_dynamic($karyawan);
	$view .= form_field_display( $form_karyawan  , "NIK/Nama Karyawan"  );
	
	$bulans  = array(
		'01'=>'01', 
		'02'=>'02', 
		'03'=>'03', 
		'04'=>'04', 
		'05'=>'05', 
		'06'=>'06', 
		'07'=>'07', 
		'08'=>'08', 
		'09'=>'09', 
		'10'=>'10', 
		'11'=>'11', 
		'12'=>'12', 
	);
	$bulan = array(
		'name'=>'bulan',
		'value'=>isset($_POST['bulan']) ? $_POST['bulan']: $month  
	);
	$bulan_form = form_dropdown( $bulan,$bulans);
	
	$tahun = array(
		'name'=>'tahun','id'=>'tahun','size'=>5,
		'value'=>isset($_POST['tahun']) ? $_POST['tahun']: $year
	); 
	$tahun_form = form_dynamic($tahun );
	$view .= form_field_display( $bulan_form.' '. $tahun_form  .'<input type="button" onclick="javascript:checkValue();"  value=" Chk "/>' , "Periode Mulai"    );
	
	$bulan_s = array(
		'name'=>'bulan_s','id'=>'bulan_s',
		'value'=>isset($_POST['bulan_s']) ? $_POST['bulan_s']: $month_s  
	);
	$bulan_s_form = form_dropdown( $bulan_s,$bulans);
	
	$tahun_s = array(
		'id'=>'tahun_s','name'=>'tahun_s','size'=>5,
		'value'=>isset($_POST['tahun_s']) ? $_POST['tahun_s']: $year_s
	); 
	$tahun_s_form = form_dynamic($tahun_s );
	$view .= form_field_display( $bulan_s_form.' '. $tahun_s_form  , "Periode Selesai"    );

	$total = array(
				'name'=>'total',
				'value'=>( isset($_POST['total']) ? $_POST['total'] : rp_format($fields['total']))  ,
				'id'=>'total',
				'type'=>'text',
				'size'=>'20'
			);
	$form_field_total = form_money($total); 
	$view .= form_field_display( $form_field_total   , "Total"   );
	$bulanan = array(
				'name'=>'bulanan',
				'value'=>( isset($_POST['bulanan']) ? $_POST['bulanan'] :  $fields['potongan'] ),
				'id'=>'bulanan',
				'type'=>'text',
				'size'=>'20'
			);
	$form_field_bulanan= form_dynamic($bulanan); 
	$view .= form_field_display( $form_field_bulanan   , "Pot / bulan"   );
	$frekuensi = array(
				'name'=>'frekuensi',
				'value'=>( isset($_POST['frekuensi']) ? $_POST['frekuensi'] :  $fields['waktu']  ),
				'id'=>'frekuensi',
				'type'=>'text',
				'size'=>'5'
			);
	$form_field_frekuensi= form_dynamic($frekuensi); 
	$view .= form_field_display( $form_field_frekuensi.' bulan'   , "Frekuensi"   );

	if($pot_aktif=='Y'){ 
		$aktif = array(
			'name'=>'aktif',
			'id'=>'aktif',
			'type'=>'checkbox',
			'checked'=>'checked'
		);
	}else{
		$aktif = array(
			'name'=>'aktif',
			'id'=>'aktif',
			'type'=>'checkbox',
		); 
	}
	$form_aktif = form_dynamic($aktif);	
	$view .= form_field_display( $form_aktif .'<font size="2">Uncheck untuk percepatan</font>', "Aktif" );
	
	$submit = array(
		'value' => ( $id ==0 ? ' Simpan ' :'  Update  '),
		'name' => 'simpan', 
		'type'=>'submit','class'=>'main_button'
	);
	$form_submit= form_dynamic($submit); 
	
	$view .= form_field_display( $form_submit  , "&nbsp;" ,  "" );
	$view .= form_footer( );
	return $view;
}

function submit_potongan_limit($id = 0){
	//$waktu=($_POST['tahun_s']*12+$_POST['bulan_s'])-($_POST['tahun']*12+$_POST['bulan'])+1;
	$total=set_text_as_number($_POST['total'])	;
	$bulanan=set_text_as_number($_POST['bulanan'])	;
	//$potongan=$total/$waktu;
	$aktif = isset($_POST['aktif']) ? 'Y': 'T';
	$tanggal_mulai = $_POST['tahun'].'-'.$_POST['bulan'].'-01';
	$tanggal_selesai = $_POST['tahun_s'].'-'.$_POST['bulan_s'].'-01';	
	$karyawan_id = check_karyawan_by_nik_nama($_POST['nama_karyawan']);
	$datas = array(
		'komponen_gaji_id'=>my_type_data_int($_POST['komponen_gaji_id']),
		'karyawan_id'=> my_type_data_int($karyawan_id),
		'tanggal_mulai'=> my_type_data_str($tanggal_mulai),
		'tanggal_selesai'=> my_type_data_str($tanggal_selesai),
		'total'=>my_type_data_str($total ),
		'potongan'=> my_type_data_str($bulanan),
		'waktu'=> my_type_data_int($_POST['frekuensi']),
		'aktif'=> my_type_data_str($aktif),
	);
	
	if($id > 0){
		return my_update_record( 'pr_potongan_limit' , 'potongan_limit_id' , $id , $datas);
	}
	
	return my_insert_record( 'pr_potongan_limit' , $datas );
}

function potongan_limit_detail($id,$kspes,$knik,$kbulan,$ktahun,$kstatus){
	$header = array(
		'Periode'=>array('style'=>'text-align:center;border-bottom:2px solid;width:20%'),  
		'Potongan'=>array('style'=>'text-align:right;border-bottom:2px solid;width:40%'),  
		'Sisa'=>array('style'=>'text-align:right;border-bottom:2px solid;width:40%'),  
	);
	$fields = my_get_data_by_id('pr_potongan_limit','potongan_limit_id',$id);
	$sisa=$fields['total'];
	$tahun =date('Y',strtotime($fields['tanggal_mulai']));	
	$bulan = date('m',strtotime($fields['tanggal_mulai']));
	$row = array(); 
	$row[] = array(
		'periode'=>position_text_align ('' ,'center'),    
		'potongan'=>  position_text_align (rp_format(0) ,'right'),     
		'sisa'=>  position_text_align (rp_format($sisa),'right'),     
	);	
	for($i = 0; $i<($fields['waktu']); $i++){
		$sisa-=$fields['potongan'] ;
		$bulan = sprintf('%02d' , $bulan);
	
		$row[] = array(
			'periode'=>position_text_align (sprintf('%02d' , $bulan).' - '.$tahun ,'center'),    
			'potongan'=>  position_text_align (rp_format($fields['potongan']) ,'right'),    
			'sisa'=>  position_text_align (rp_format($sisa),'right'),        
		);
		if($bulan<12)$bulan++;
		else{
			$bulan=1;
			$tahun++; 
		}		
	}
	$datas = table_rows($row);
	
	$potongan_limits=my_get_data_by_id('pr_potongan_limit','potongan_limit_id',$id) ;
	$komponen_gaji_id=isset( $potongan_limits['komponen_gaji_id'] ) ? $potongan_limits['komponen_gaji_id'] :  0;
	$spesifikasis=my_get_data_by_id('pr_komponen_gaji','komponen_gaji_id',$komponen_gaji_id) ;
	$karyawan_id=isset( $potongan_limits['karyawan_id'] ) ? $potongan_limits['karyawan_id'] :  0;
	$karyawan=loaddata_karyawan( $karyawan_id);
	$view  = form_header( "..." , "..."  );
	$view .= form_field_display( '<span class="label_form"> '.$spesifikasis['nama_komponen_gaji'].'</span>'   , "<b>Spesifikasi</b>"    ); 
	$view .= form_field_display( '<span class="label_form"> '.$karyawan['nik'].'/'.$karyawan['nama_gelar'].'</span>'   , "<b>NIK / Nama Karyawan</b>"    ); 
	$view .= form_field_display( '<span class="label_form"> '.
		date("m-Y", strtotime($potongan_limits['tanggal_mulai'] ." 00:00:00" ) ).'/'.
		date("m-Y", strtotime($potongan_limits['tanggal_selesai'] ." 00:00:00" ) ).'</span>'   , "<b>Mulai / Selesai</b>"    ); 
	
	$menuontop = array(  
		'Excel' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_potongan_limit&task=detail_excel&id='.$id.'\''),
		'Back' => array('onclick'=>'javascript:location.href=\'index.php?com=pr_potongan_limit&kspes='.$kspes.'&knik='.$kbulan.'&kbulan='.$kbulan.'&ktahun='.$ktahun.'&kstatus='.$kstatus.'\''),
	);
	$printmenu = additional_menu_on_list( $menuontop , '');
	return $view.$printmenu.table_builder( 	$header , 	$datas , 	5 , 	false );
}

function delete_potongan_limit($id){
	$query = "DELETE FROM pr_potongan_limit WHERE potongan_limit_id={$id}";
	return my_query($query);
}

function excel_report_pot_limit($kspes,$knik,$kbulan,$ktahun,$kstatus){
	my_component_load('xl_builder' , false);
	$header = array(
		'#'=>array('style'=>'border-bottom:2px solid;width:5%'),  
		'Spesifikasi'=>array('style'=>'border-bottom:2px solid;width:25%'),  
		'NIK'=>array('style'=>'border-bottom:2px solid;width:9%'),  
		'Karyawan'=>array('style'=>'border-bottom:2px solid;width:20%'),  
		'Mulai'=>array('style'=>'border-bottom:2px solid;width:7%'),  
		'Selesai'=>array('style'=>'border-bottom:2px solid;width:7%'),  
		'Total'=>array('style'=>'text-align:right;border-bottom:2px solid;width:10%'),  
		'Status'=>array('style'=>'text-align:right;border-bottom:2px solid;width:9%'),  
	);
	$query = "SELECT * ,a.tanggal_selesai AS tgl_end FROM pr_potongan_limit a
			LEFT JOIN pr_komponen_gaji b ON a.komponen_gaji_id=b.komponen_gaji_id 
			INNER JOIN   karyawan c  ON a.karyawan_id = c.karyawan_id
			INNER JOIN biodata d ON d.biodata_id = c.biodata_id
			WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
			AND b.model_entry = 6";		
	if($kspes<>0){
		$query .= " AND a.komponen_gaji_id={$kspes}";
	}
	if($knik<>0){
		$query .= " AND c.nik={$knik}";
	}
	if($kbulan<>0 AND $ktahun<>''){
		$query .= " AND ('{$ktahun}-{$kbulan}-01' BETWEEN a.tanggal_mulai AND a.tanggal_selesai)";
	}
	$periode_gaji_id =get_periode_gaji();
	$periodes = my_get_data_by_id('pr_periode','periode_id',$periode_gaji_id);
	if($kstatus==1){
		$query .= " AND a.tanggal_selesai<'{$periodes['tanggal']}'";
	}
	if($kstatus==2){
		$query .= " AND a.tanggal_selesai>='{$periodes['tanggal']}' AND a.aktif='Y'";
	}	
	if($kstatus==3){
		$query .= " AND a.tanggal_selesai>='{$periodes['tanggal']}' AND a.aktif<>'Y'";
	}	
	$query .= " ORDER BY c.nik ASC ";
	$result = my_query($query ); 
	$row = array();  
	$i=1;
	while($ey = my_fetch_array($result)){
		$karyawan=loaddata_karyawan( $ey['karyawan_id']); 
		if( $ey['tgl_end'] < $periodes['tanggal']){
				$status="Lunas" ;
		}else{
			if($ey['aktif']=="Y"){
				$status="Belum Lunas" ;
			}else{
				$status="Percepatan" ;			
			}
		}		
		
		$row[] = array(
			'#'=>position_text_align ($i, 'center'), 
			'spec'=>  $ey['nama_komponen_gaji'] ,   
			'nik'=>  $karyawan['nik'] ,   
			'nama'=>  $karyawan['nama_gelar'] ,   
			'mulai'=>  position_text_align (  $ey['tanggal_mulai']  , 'left'), 
			'selesai'=>  position_text_align (  $ey['tgl_end']   , 'left'), 
			'total'=>  position_text_align( $ey['total'] ,'right') ,
			'aktif'=> position_text_align( $status,'center') , 
		);
		 $i++;
	}
	
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas , 15 ,false ); 
}

function print_plain($kspes,$knik,$kbulan,$ktahun,$kstatus){
	
	$query = "SELECT * ,a.tanggal_selesai AS tgl_end FROM pr_potongan_limit a
			LEFT JOIN pr_komponen_gaji b ON a.komponen_gaji_id=b.komponen_gaji_id 
			INNER JOIN   karyawan c  ON a.karyawan_id = c.karyawan_id
			INNER JOIN biodata d ON d.biodata_id = c.biodata_id
			WHERE c.karyawan_id NOT IN (select karyawan_id FROM karyawan_disable) 
			AND b.model_entry = 6";		
	if($kspes<>0){
		$query .= " AND a.komponen_gaji_id={$kspes}";
	}
	if($knik<>0){
		$query .= " AND c.nik={$knik}";
	}
	if($kbulan<>0 AND $ktahun<>''){
		$query .= " AND ('{$ktahun}-{$kbulan}-01' BETWEEN a.tanggal_mulai AND a.tanggal_selesai)";
	}
	$periode_gaji_id =get_periode_gaji();
	$periodes = my_get_data_by_id('pr_periode','periode_id',$periode_gaji_id);
	$tgl_gaji=$periodes['tanggal'] ;	
	if($kstatus==1){
		$query .= " AND a.tanggal_selesai<'{$tgl_gaji}'";
	}
	if($kstatus==2){
		$query .= " AND a.tanggal_selesai>='{$tgl_gaji}' AND a.aktif='Y'";
	}	
	if($kstatus==3){
		$query .= " AND a.tanggal_selesai>='{$tgl_gaji}' AND a.aktif<>'Y'";
	}	
	$query .= " ORDER BY c.nik ASC ";
	$result = my_query($query ); 
	$datas = array();  
	$datas[] = array(
		 'k1'=>array("No." ,5 ,STR_PAD_BOTH),
		 'k2'=>array("Spesifikasi" ,25,STR_PAD_RIGHT),
		 'k3'=>array("NIK" ,10,STR_PAD_RIGHT),
		 'k4'=>array("Karyawan" ,20,STR_PAD_RIGHT),  
		 'k5'=>array("Mulai" ,10,STR_PAD_BOTH),  
		 'k6'=>array("Selesai" ,10,STR_PAD_BOTH),  
		 'k7'=>array("Total" ,10,STR_PAD_BOTH),  
		 'k8'=>array("Status" ,10,STR_PAD_BOTH),  
	); 
	$datas[] = array(	//FOR HEADER SPACE (NECK)
		 'k1'=>array(" " ,100,STR_PAD_BOTH),
	); 
	$i=0;
	while($ey = my_fetch_array($result)){
	if( $ey['tgl_end'] < $periodes['tanggal']){
				$status="Lunas" ;
		}else{
			if($ey['aktif']=="Y"){
				$status="Belum Lunas" ;
			}else{
				$status="Percepatan" ;			
			}
		}	
		$karyawan=loaddata_karyawan( $ey['karyawan_id']); 
		$i++; 
		
		$datas[] = array(		 
			'k1'=>array($i ,5 ,STR_PAD_BOTH),
			'k2'=>array($ey['nama_komponen_gaji'],25,STR_PAD_RIGHT),
			'k3'=>array($karyawan['nik'] ,10,STR_PAD_RIGHT),
			'k4'=>array($karyawan['nama_gelar'] ,20,STR_PAD_RIGHT),  
			'k5'=>array($ey['tanggal_mulai'] ,10,STR_PAD_BOTH),  
			'k6'=>array($ey['tgl_end'] ,10,STR_PAD_BOTH),  
			'k7'=>array(rp_format($ey['total']),10,STR_PAD_LEFT),  
			'k8'=>array($status, 10,STR_PAD_BOTH), 
		);
	}
	$view = 	str_pad("CETAK LAPORAM LIMIT WAKTU",100," ",STR_PAD_BOTH);
	return $view ."\n"."\n".set_txt_report($datas);
}


function potongan_limit_detail_excel($id){
	$header = array(
		'Periode'=>array('style'=>'text-align:center;border-bottom:2px solid;width:20%'),  
		'Potongan'=>array('style'=>'text-align:right;border-bottom:2px solid;width:40%'),  
		'Sisa'=>array('style'=>'text-align:right;border-bottom:2px solid;width:40%'),  
	);
	$fields = my_get_data_by_id('pr_potongan_limit','potongan_limit_id',$id);
	$sisa=$fields['total'];
	$tahun =date('Y',strtotime($fields['tanggal_mulai']));	
	$bulan = date('m',strtotime($fields['tanggal_mulai']));
	$row = array(); 
	$row[] = array(
		'periode'=>  '',    
		'potongan'=>    (0) ,      
		'sisa'=>    ($sisa),     
	);	
	for($i = 0; $i<($fields['waktu']); $i++){
		$sisa-=$fields['potongan'] ;
		$bulan = sprintf('%02d' , $bulan);
	
		$row[] = array(
			'periode'=> sprintf('%02d' , $bulan).' - '.$tahun ,    
			'potongan'=>   ($fields['potongan'])  ,    
			'sisa'=>   ($sisa),        
		);
		if($bulan<12)$bulan++;
		else{
			$bulan=1;
			$tahun++; 
		}		
	}
	$datas = table_rows_excel($row); 
	return table_builder_excel($header , $datas ,4 ,false ); 
}
